/**
 * 文件：CommentServiceImpl.java
 * 作者：苏新阳
 * 完成度：完成
 * 最后修改日期：2023-7-3 01:00
 */
package com.gomokult.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import com.gomokult.dao.CommentDAO;
import com.gomokult.dao.GameDAO;
import com.gomokult.dao.impl.CommentDAOImpl;
import com.gomokult.dao.impl.GameDAOImpl;
import com.gomokult.dao.impl.UserDAOImpl;
import com.gomokult.entity.Comment;
import com.gomokult.entity.Game;
import com.gomokult.exception.InvalidIDException;
import com.gomokult.exception.NoSuchPageException;
import com.gomokult.exception.IllegalOperationException;
import com.gomokult.service.CommentService;

/**
 * 评论服务接口（{@code CommentService}）的实现类。
 * @author 苏新阳
 */
public class CommentServiceImpl implements CommentService {
	private CommentServiceImpl() {}
	private static final CommentServiceImpl singleton = new CommentServiceImpl();
	public static CommentServiceImpl getSingleton() { return singleton; }

    private CommentDAO commentDAO = CommentDAOImpl.getSingleton();   //注入CommentDAO
    private GameDAO gameDAO = GameDAOImpl.getSingleton();       //注入GameDAO


    @Override
    public Comment getComment(int commentID) throws InvalidIDException {
        return commentDAO.selectComment(commentID);    //调用CommentDAO获取评论
    }

    @Override
    public int getCommentsCount(int gameID) throws InvalidIDException {
        return commentDAO.selectCommentsCount(gameID);     //调用CommentDAO获取评论总数
    }

    @Override
    public int getCommentPagesCount(int gameID) throws InvalidIDException {
        return commentDAO.selectCommentPagesCount(gameID);     //调用CommentDAO获取评论页数
    }

    @Override
    public List<Comment> getCommentsFromPage(int gameID, int page) throws InvalidIDException, NoSuchPageException {
        return commentDAO.selectCommentsFromPage(gameID, page);     //调用CommentDAO获取评论列表
    }

    //根据userId判断是否可以删除，commentID的评论
    @Override
    public void deleteComment(int userID, int commentID) throws InvalidIDException, IllegalOperationException {
        Comment comment = commentDAO.selectComment(commentID);
        if (checkPermission(userID, comment.getGame())) {
            commentDAO.deleteComment(commentID);
        } else if (userID == comment.getPublisher().getUserID()) {
            commentDAO.deleteComment(commentID);
        } else {
            throw new IllegalOperationException();
        }
    }

    @Override
    public void clearComment(int userID, int gameID) throws InvalidIDException, IllegalOperationException {
        if (checkPermission(userID, gameID)) {
            commentDAO.clearComment(gameID);
        } else {
            throw new IllegalOperationException();
        }
    }


    @Override
    public int publishComment(int userID, int gameID, String content, int parentID,int referencedRounds) throws InvalidIDException {

        Comment comment = new Comment();  //构建评论对象
        comment.setContent(content);
        comment.setPublisher(UserDAOImpl.getSingleton().selectUserbyID(userID));
        comment.setParentComment(CommentServiceImpl.getSingleton().getComment(parentID));
        comment.setGame(gameID);
        comment.setReferencedRound(referencedRounds);
        comment.setDate(new Timestamp(new Date().getTime()));
        commentDAO.insertComment(comment);;   //调用CommentDAO发布评论
        return comment.getCommentID();//返回评论id，在数据库中自动生成
    }

    @Override
    public int publishComment(int userID, int gameID, String content) throws InvalidIDException {
         Comment comment = new Comment();  //构建评论对象
         comment.setContent(content);
         comment.setPublisher(UserDAOImpl.getSingleton().selectUserbyID(userID));
         comment.setParentComment(null);
         comment.setGame(gameID);
         comment.setDate(new Timestamp(new Date().getTime()));
         commentDAO.insertComment(comment);   //调用CommentDAO发布评论
         return commentDAO.selectCommentId(userID,gameID,comment.getDate());//返回评论id，在数据库中自动生成
     }

    //@Override
    //public void setCommentable(int userID, int gameID, boolean commentable) throws InvalidIDException, IllegalOperationException {
    //    if (checkPermission(userID, gameID)) {   //检查用户权限
    //        Game game = gameDAO.getGame(gameID);
    //        game.setCommentable(commentable);   //设置游戏评论区状态
    //        gameDAO.updateGame(game);   //调用GameDAO更新游戏
    //    } else {
    //        throw new IllegalOperationException();  //无权限抛出异常
    //    }
    //}

    //@Override
    //public boolean getCommentable(int gameID) throws InvalidIDException {
    //    return gameDAO.getGame(gameID).isCommentable();   //调用GameDAO获取游戏评论区状态
    //}

    private boolean checkPermission(int userID, int gameID) throws InvalidIDException {
        Game game = gameDAO.getGame(gameID);   //获取游戏
        return game.getUserBlack().getUserID() == userID || game.getUserWhite().getUserID() == userID;  //检查用户是否为游戏参与者
    }
}